package ext.tianma.masschange.processor;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Vector;

import wt.change2.WTChangeActivity2;
import wt.fc.PersistenceServerHelper;
import wt.fc.QueryResult;
import wt.part.LineNumber;
import wt.part.Quantity;
import wt.part.QuantityUnit;
import wt.part.SubstituteQuantity;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.part.WTPartSubstituteLink;
import wt.part.WTPartUsageLink;
import wt.session.SessionHelper;
import wt.util.WTException;
import wt.util.WTProperties;
import wt.util.WTPropertyVetoException;

import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.netmarkets.util.beans.NmCommandBean;

import ext.com.core.CoreUtil;
import ext.com.iba.IBAUtil;
import ext.tianma.masschange.log.SubstituteMassChangeLog;
import ext.tianma.masschange.util.MassChangeProcessorUtil;
import ext.tianma.masschange.util.MassChangeUtil;

public class SubstituteMassChangeFormProcessor extends DefaultObjectFormProcessor {
	private static String YIELD = "Yield";
	private static String RESOURCE = "ext.tianma.masschange.resource.MassChangeResource";
	private static String CLASSNAME="";
	private static boolean VERBOSE = false;
	private static WTProperties wtProperties;
	private static Locale locale = null;
	private static String MASSCHANGE_RESULT_TABLEID1 = "ext.tianma.masschange.builder.SubstituteMassChangeSearchResultTableBuilder";
	private static String MASSCHANGE_RESULT_TABLEID2 = "ext.tianma.masschange.builder.NoYeildMassChangeSearchResultTableBuilder";
	static {

		try {
			CLASSNAME= SubstituteMassChangeFormProcessor.class.getName();
			wtProperties = WTProperties.getLocalProperties();
			locale = SessionHelper.manager.getLocale();
			ResourceBundle.getBundle("ext.tianma.masschange.resource.MassChangeResource", locale);
			VERBOSE = wtProperties.getProperty("ext.tianma.masschange.processor.verbose", false);
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		}catch (Throwable throwable) {
			throw new ExceptionInInitializerError(throwable);
		}
	}
	
	public SubstituteMassChangeFormProcessor() {

	}
	
	 /**
	 *输出log信息
	 * @throws WTException 
	 */
	private static void outLogInfo(String obj) throws WTException {
		SubstituteMassChangeLog.writeLog(obj);
	}
	
	private static void outLogWarn(String obj) throws WTException {
		SubstituteMassChangeLog.writeWarnLog(obj);
	}
	
	/**
	 *输出log信息
	 * @throws WTException 
	 */
	private static void outLogInfo(String action, String obj) throws WTException {
		SubstituteMassChangeLog.writeLog(action, obj);
	}
	
	 /**
	 *输出调试信息
	 */
	private static void outDebugInfo(String info) {
		if(VERBOSE)
			System.out.println(CLASSNAME+"......"+info);
	}
	
	public FormResult doOperation(NmCommandBean nmcommandbean, List list) throws WTException { 
		
		outDebugInfo("Start SubstituteMassChangeFormProcessor");
	    FormResult formresult = new FormResult();
	    formresult.setStatus(FormProcessingStatus.SUCCESS);
	    
	    WTChangeActivity2 ca = MassChangeProcessorUtil.getCA(nmcommandbean);
	   
	    outDebugInfo("ca = "+ca);
	    if(ca!=null) {
    		//获取table中勾选的数据
    	    	    
		    //获取MassChange类型
		    String SUBSTITUTE_MASSCHANGE_TYPE_VALUE = (String) (nmcommandbean.getText().get("SUBSTITUTE_MASSCHANGE_TYPE"));
		    outDebugInfo("SUBSTITUTE_MASSCHANGE_TYPE_VALUE = "+SUBSTITUTE_MASSCHANGE_TYPE_VALUE);
		    
		    if(SUBSTITUTE_MASSCHANGE_TYPE_VALUE.equalsIgnoreCase("批量更换主料")) {
		    	String OLD_MAIN_MATERIAL_NUMBER_VALUE = (String)(nmcommandbean.getText().get("OLD_MAIN_MATERIAL$label$"));
		    	String OLD_MAIN_MATERIAL_NAME_VALUE = (String)(nmcommandbean.getText().get("OLD_MAIN_MATERIAL_NAME"));
		    	String OLD_MAIN_MATERIAL_VIEW_VALUE = (String)(nmcommandbean.getText().get("OLD_MAIN_MATERIAL_VIEW"));
		    	String NEW_MAIN_MATERIAL_NUMBER_VALUE = (String)(nmcommandbean.getText().get("NEW_MAIN_MATERIAL$label$"));
		    	String NEW_MAIN_MATERIAL_NAME_VALUE = (String)(nmcommandbean.getText().get("NEW_MAIN_MATERIAL_NAME"));
		    	String NEW_MAIN_MATERIAL_VIEW_VALUE = (String)(nmcommandbean.getText().get("NEW_MAIN_MATERIAL_VIEW"));
		    	String QUANTITY_VALUE = (String)(nmcommandbean.getText().get("QUANTITY"));
		    		    	
		    	outLogInfo("开始执行替代料批量变更...");
		    	outLogInfo("输出表单数据开始...");
		    	outLogInfo("替代料批量变更类型   - 批量更换主料");
		    	outLogInfo("原主料   - "+ OLD_MAIN_MATERIAL_NUMBER_VALUE);
		    	outLogInfo("原主料名称   - "+ OLD_MAIN_MATERIAL_NAME_VALUE);
		    	outLogInfo("原主料视图   - "+ OLD_MAIN_MATERIAL_VIEW_VALUE);
		    	outLogInfo("新主料   - "+ NEW_MAIN_MATERIAL_NUMBER_VALUE);
		    	outLogInfo("新主料名称   - "+ NEW_MAIN_MATERIAL_NAME_VALUE);
		    	outLogInfo("新主料视图   - "+ NEW_MAIN_MATERIAL_VIEW_VALUE);
		    	outLogInfo("数量   - "+ QUANTITY_VALUE);
		    	outLogInfo("输出表单数据结束...");
		    	
		    	outDebugInfo("OLD_MAIN_MATERIAL_NUMBER_VALUE = "+OLD_MAIN_MATERIAL_NUMBER_VALUE);
		    	outDebugInfo("OLD_MAIN_MATERIAL_VIEW_VALUE = "+OLD_MAIN_MATERIAL_VIEW_VALUE);
		    	outDebugInfo("NEW_MAIN_MATERIAL_NUMBER_VALUE = "+NEW_MAIN_MATERIAL_NUMBER_VALUE);
		    	outDebugInfo("NEW_MAIN_MATERIAL_VIEW_VALUE = "+NEW_MAIN_MATERIAL_VIEW_VALUE);
		    	Vector lines = MassChangeProcessorUtil.getSelectedData(nmcommandbean, MASSCHANGE_RESULT_TABLEID1);
	    	    outDebugInfo("lines.size()="+lines.size());
	    	    if(lines.size()!=0) { 	
			    	if(OLD_MAIN_MATERIAL_NUMBER_VALUE!=null && OLD_MAIN_MATERIAL_NUMBER_VALUE!="" && OLD_MAIN_MATERIAL_VIEW_VALUE!=null && OLD_MAIN_MATERIAL_VIEW_VALUE!="" &&
			    		NEW_MAIN_MATERIAL_NUMBER_VALUE!=null && NEW_MAIN_MATERIAL_NUMBER_VALUE!="" && NEW_MAIN_MATERIAL_VIEW_VALUE!=null && NEW_MAIN_MATERIAL_VIEW_VALUE!="") {
			    		WTPart oldMainMaterial = CoreUtil.getWTPartByNumberAndView(OLD_MAIN_MATERIAL_NUMBER_VALUE, OLD_MAIN_MATERIAL_VIEW_VALUE);
			    		WTPart newMainMaterial = CoreUtil.getWTPartByNumberAndView(NEW_MAIN_MATERIAL_NUMBER_VALUE, NEW_MAIN_MATERIAL_VIEW_VALUE);
			    		WTPartMaster newMainMaterialmMaster = (WTPartMaster) newMainMaterial.getMaster();
			    		Vector changeAfter = new Vector();
			    		for(int i=0; i<lines.size(); i++) {
			    			Vector line = (Vector)lines.get(i);
			    			String number = (String) line.get(0);
			    			String view = (String)line.get(1);
			    			String quantityStr = (String)line.get(2);
			    			String unit = (String)line.get(3);
			    			String yield = (String)line.get(4);
			    			String lineNumberStr=(String)line.get(5);
			    			//检出对象
			    			WTPart checkoutParent = MassChangeProcessorUtil.getCheckoutParent(number, view,ca);			    			
			    			
			    			Vector usageLinks = MassChangeUtil.getPartUsageLink(checkoutParent, oldMainMaterial);
			    			WTPartUsageLink usageLink = (WTPartUsageLink)usageLinks.get(0);
			    			//记录老的link信息
			    			Vector oldLinkValues = new Vector();
			    			//usageLink
			    			Vector usageLinkValue = new Vector();
			    			usageLinkValue.add(usageLink.getUses());
			    			usageLinkValue.add(usageLink.getQuantity().getAmount());
			    			usageLinkValue.add(usageLink.getQuantity().getUnit());
			    			oldLinkValues.add(usageLinkValue);
			    			QueryResult qr = MassChangeUtil.getPartSubstituteLinks(usageLink);
			    			//Substitutelink
			    			while (qr.hasMoreElements()) {
			    				Vector substitutelinkValue = new Vector();
			    				WTPartSubstituteLink substitutelink = (WTPartSubstituteLink) qr.nextElement();
			    				WTPartMaster SubstitutesMaster = substitutelink.getSubstitutes();
			    				if(SubstitutesMaster.equals(newMainMaterialmMaster)) {
			    					PersistenceServerHelper.manager.remove(substitutelink);
			    					continue;
			    				}
			    				double quantityAmount = substitutelink.getQuantity().getAmount();
			    				QuantityUnit quantityUnit = substitutelink.getQuantity().getUnit();
			    				substitutelinkValue.add(SubstitutesMaster);
			    				substitutelinkValue.add(quantityAmount);
			    				substitutelinkValue.add(quantityUnit);
			    				PersistenceServerHelper.manager.remove(substitutelink);
			    				oldLinkValues.add(substitutelinkValue);
			    			}
			    			PersistenceServerHelper.manager.remove(usageLink);
			    			
			    			LineNumber lineNumber=LineNumber.newLineNumber(Long.parseLong(lineNumberStr));
			    			double quantitDouble = Double.parseDouble(quantityStr);
			    			Quantity quantity=Quantity.newQuantity(quantitDouble, QuantityUnit.toQuantityUnit(unit));
			    			WTPartUsageLink newUsageLink = WTPartUsageLink.newWTPartUsageLink(checkoutParent, newMainMaterialmMaster);
			    			newUsageLink.setQuantity(quantity);
			    			try {
								newUsageLink.setLineNumber(lineNumber);
							} catch (WTPropertyVetoException e) {
								e.printStackTrace();
							}
			    			PersistenceServerHelper.manager.insert(newUsageLink);
			    			IBAUtil.forceSetIBAValue(newUsageLink, YIELD, yield);
			    			//原使用关系
			    			for(int j=0; j<oldLinkValues.size(); j++) {
				    			Vector uselinkValue = (Vector) oldLinkValues.get(j);
				    			WTPartMaster master0 = (WTPartMaster) uselinkValue.get(0);
				    			double quantity0 = (Double) uselinkValue.get(1);
				    			QuantityUnit unit0 = (QuantityUnit) uselinkValue.get(2);
				    			SubstituteQuantity substituteQuantity0 = SubstituteQuantity.newSubstituteQuantity(quantity0, unit0);
				    			WTPartSubstituteLink substitutelink0 = WTPartSubstituteLink.newWTPartSubstituteLink(newUsageLink, master0);
				    			substitutelink0.setQuantity(substituteQuantity0);
				    			PersistenceServerHelper.manager.insert(substitutelink0);
			    			}
			    			
			    			CoreUtil.checkinObject(checkoutParent, "MASSCHANGE_REPLACE_MAIN");
			    			changeAfter.add(checkoutParent);
			    			
			    			//输出数据处理log
		    				StringBuffer stringbuffer = new StringBuffer();
			    			stringbuffer.append("父件[编码:"+number+", 名称:"+checkoutParent.getName()+", 视图:"+view +"]");
			    			stringbuffer.append(" - ");
			    			stringbuffer.append("新主料[编码:"+NEW_MAIN_MATERIAL_NUMBER_VALUE +", 名称:"+NEW_MAIN_MATERIAL_NAME_VALUE+", 视图:"+NEW_MAIN_MATERIAL_VIEW_VALUE +"]");
			    			stringbuffer.append("替换");
			    			stringbuffer.append("原主料[编码:"+OLD_MAIN_MATERIAL_NUMBER_VALUE +", 名称:"+OLD_MAIN_MATERIAL_NAME_VALUE+", 视图:"+OLD_MAIN_MATERIAL_VIEW_VALUE +"]");
			    			stringbuffer.append("[数量:"+quantityStr+", 单位:"+ QuantityUnit.toQuantityUnit(unit).getDisplay(locale)+",行号:"+lineNumberStr+", 良率:"+yield+"]");
			    			outLogInfo(stringbuffer.toString());
			    		}
			    		//store changeAfter
						try {
							MassChangeUtil.addChangeObjs(ca,changeAfter,"wt.change2.ChangeRecord2");
						} catch (Exception e) {
							e.printStackTrace();
						}
			    	}
	    	    }
	    	    outLogInfo("结束执行替代料批量变更...");
			} else if(SUBSTITUTE_MASSCHANGE_TYPE_VALUE.equalsIgnoreCase("批量更换替代料")) {
		    	String OLD_SUBSTITUTE_MATERIAL_NUMBER_VALUE = (String)(nmcommandbean.getText().get("OLD_SUBSTITUTE_MATERIAL$label$"));
		    	String OLD_SUBSTITUTE_MATERIAL_NAME_VALUE = (String)(nmcommandbean.getText().get("OLD_SUBSTITUTE_MATERIAL_NAME"));
		    	String OLD_SUBSTITUTE_MATERIAL_VIEW_VALUE = (String)(nmcommandbean.getText().get("OLD_SUBSTITUTE_MATERIAL_VIEW"));
		    	String NEW_SUBSTITUTE_MATERIAL_NUMBER_VALUE = (String)(nmcommandbean.getText().get("NEW_SUBSTITUTE_MATERIAL$label$"));
		    	String NEW_SUBSTITUTE_MATERIAL_NAME_VALUE = (String)(nmcommandbean.getText().get("NEW_SUBSTITUTE_MATERIAL_NAME"));
		    	String NEW_SUBSTITUTE_MATERIAL_VIEW_VALUE = (String)(nmcommandbean.getText().get("NEW_SUBSTITUTE_MATERIAL_VIEW"));
		    	String MAIN_MATERIAL_NUMBER_VALUE = (String)(nmcommandbean.getText().get("MAIN_MATERIAL$label$"));
		    	String MAIN_MATERIAL_NAME_VALUE = (String)(nmcommandbean.getText().get("MAIN_MATERIAL_NAME"));
		    	String MAIN_MATERIAL_VIEW_VALUE = (String)(nmcommandbean.getText().get("MAIN_MATERIAL_VIEW"));
		    	String QUANTITY_VALUE = (String)(nmcommandbean.getText().get("QUANTITY"));
		    	
		    	outLogInfo("开始执行替代料批量变更...");
		    	outLogInfo("输出表单数据开始...");
		    	outLogInfo("替代料批量变更类型   - 批量更换替代料");
		    	outLogInfo("原替代料   - "+ OLD_SUBSTITUTE_MATERIAL_NUMBER_VALUE);
		    	outLogInfo("原替代料名称   - "+ OLD_SUBSTITUTE_MATERIAL_NAME_VALUE);
		    	outLogInfo("原替代料视图   - "+ OLD_SUBSTITUTE_MATERIAL_VIEW_VALUE);
		    	outLogInfo("新替代料   - "+ NEW_SUBSTITUTE_MATERIAL_NUMBER_VALUE);
		    	outLogInfo("新替代料名称   - "+ NEW_SUBSTITUTE_MATERIAL_NAME_VALUE);
		    	outLogInfo("新替代料视图   - "+ NEW_SUBSTITUTE_MATERIAL_VIEW_VALUE);
		    	outLogInfo("主料   - "+ MAIN_MATERIAL_NUMBER_VALUE);
		    	outLogInfo("主料名称   - "+ MAIN_MATERIAL_NAME_VALUE);
		    	outLogInfo("主料视图   - "+ MAIN_MATERIAL_VIEW_VALUE);
		    	outLogInfo("数量   - "+ QUANTITY_VALUE);
		    	outLogInfo("输出表单数据结束...");
		    	Vector lines = MassChangeProcessorUtil.getNoYieldSelectedData(nmcommandbean, MASSCHANGE_RESULT_TABLEID2);
	    	    outDebugInfo("lines.size()="+lines.size());
	    	    if(lines.size()!=0) {
		    	
			    	if(OLD_SUBSTITUTE_MATERIAL_NUMBER_VALUE!=null && OLD_SUBSTITUTE_MATERIAL_NUMBER_VALUE!="" && OLD_SUBSTITUTE_MATERIAL_VIEW_VALUE!="" && OLD_SUBSTITUTE_MATERIAL_VIEW_VALUE!=null &&
			    		NEW_SUBSTITUTE_MATERIAL_NUMBER_VALUE!=null && NEW_SUBSTITUTE_MATERIAL_NUMBER_VALUE!="" && NEW_SUBSTITUTE_MATERIAL_VIEW_VALUE!=null && NEW_SUBSTITUTE_MATERIAL_VIEW_VALUE!="" &&
			    		MAIN_MATERIAL_NUMBER_VALUE!=null && MAIN_MATERIAL_NUMBER_VALUE!="" && MAIN_MATERIAL_VIEW_VALUE!=null && MAIN_MATERIAL_VIEW_VALUE!="") {
			    		
			    		WTPart oldSubstituteMaterial = CoreUtil.getWTPartByNumberAndView(OLD_SUBSTITUTE_MATERIAL_NUMBER_VALUE, OLD_SUBSTITUTE_MATERIAL_VIEW_VALUE);
			    		WTPart newSubstituteMaterial = CoreUtil.getWTPartByNumberAndView(NEW_SUBSTITUTE_MATERIAL_NUMBER_VALUE, NEW_SUBSTITUTE_MATERIAL_VIEW_VALUE);
			    		WTPart mainMaterial = CoreUtil.getWTPartByNumberAndView(MAIN_MATERIAL_NUMBER_VALUE, MAIN_MATERIAL_VIEW_VALUE);
			    		WTPartMaster oldSubstituteMaterialMaster = (WTPartMaster) oldSubstituteMaterial.getMaster();
			    		WTPartMaster newSubstituteMaterialMaster = (WTPartMaster) newSubstituteMaterial.getMaster();
			    		
			    		Vector changeAfter = new Vector();
	    	    		for(int i=0; i<lines.size(); i++) {
	    	    			Vector line = (Vector)lines.get(i);
			    			String number = (String) line.get(0);
			    			String view = (String)line.get(1);
			    			String quantityStr = (String)line.get(2);
			    			String unit = (String)line.get(3);
			    			//String yield = (String)line.get(4);
			    			//检出对象
			    			WTPart parent = CoreUtil.getWTPartByNumberAndView(number, view);
			    			WTPart checkoutParent = null;		    			
			    			
			    			Vector usageLinks = MassChangeUtil.getPartUsageLink(parent, mainMaterial);
			    			WTPartUsageLink usageLink = (WTPartUsageLink)usageLinks.get(0);
			    			QueryResult qr = MassChangeUtil.getPartSubstituteLinks(usageLink);
			    			//记录是否有删除
			    			boolean flag = false;
			    			while(qr.hasMoreElements()) {
			    				WTPartSubstituteLink substitutelink = (WTPartSubstituteLink) qr.nextElement();
			    				WTPartMaster SubstitutesMaster = substitutelink.getSubstitutes();
			    				if(SubstitutesMaster.equals(oldSubstituteMaterialMaster)) {
			    					//PersistenceServerHelper.manager.remove(substitutelink);
			    					flag = true;
			    					break;
			    				}
			    			}
			    			if(flag) {
			    				
			    				checkoutParent = MassChangeProcessorUtil.getCheckoutParent(number, view,ca);
			    				Vector usageLinks1 = MassChangeUtil.getPartUsageLink(checkoutParent, mainMaterial);
				    			WTPartUsageLink usageLink1 = (WTPartUsageLink)usageLinks1.get(0);
				    			QueryResult qr1 = MassChangeUtil.getPartSubstituteLinks(usageLink1);
				    			//记录是否有删除
				    			while(qr1.hasMoreElements()) {
				    				WTPartSubstituteLink substitutelink = (WTPartSubstituteLink) qr1.nextElement();
				    				WTPartMaster SubstitutesMaster = substitutelink.getSubstitutes();
				    				if(SubstitutesMaster.equals(oldSubstituteMaterialMaster)) {
				    					PersistenceServerHelper.manager.remove(substitutelink);
				    					break;
				    				}
				    			}
				    			double quantitDouble = Double.parseDouble(quantityStr);
				    			SubstituteQuantity substituteQuantity0 = SubstituteQuantity.newSubstituteQuantity(quantitDouble, QuantityUnit.toQuantityUnit(unit));
				    			WTPartSubstituteLink substitutelink0 = WTPartSubstituteLink.newWTPartSubstituteLink(usageLink1, newSubstituteMaterialMaster);
				    			substitutelink0.setQuantity(substituteQuantity0);
				    			PersistenceServerHelper.manager.insert(substitutelink0);
			    			} else {
			    				
			    				//输出数据处理log
			    				StringBuffer stringbuffer = new StringBuffer();
				    			stringbuffer.append("父件[编码:"+number+", 名称:"+parent.getName()+", 视图:"+view +"]");
				    			stringbuffer.append(" - ");
				    			stringbuffer.append("主料[编码:"+MAIN_MATERIAL_NUMBER_VALUE +", 名称:"+MAIN_MATERIAL_NAME_VALUE+", 视图:"+MAIN_MATERIAL_VIEW_VALUE +"]");
				    			stringbuffer.append("父子关系中   - ");
				    			stringbuffer.append("原替代料[编码:"+OLD_SUBSTITUTE_MATERIAL_NUMBER_VALUE +", 名称:"+OLD_SUBSTITUTE_MATERIAL_NAME_VALUE+", 视图:"+OLD_SUBSTITUTE_MATERIAL_VIEW_VALUE +"]");
				    			stringbuffer.append("非父子关系中的特定替代料");
				    			outLogWarn(stringbuffer.toString());
			    				continue;
			    				
			    			}
			    			CoreUtil.checkinObject(checkoutParent, "MASSCHANGE_REPLACE_SUBSTITUTE");	
			    			changeAfter.add(checkoutParent);
			    			
			    			//输出数据处理log
		    				StringBuffer stringbuffer = new StringBuffer();
			    			stringbuffer.append("父件[编码:"+number+", 名称:"+checkoutParent.getName()+", 视图:"+view +"]");
			    			stringbuffer.append(" - ");
			    			stringbuffer.append("主料[编码:"+MAIN_MATERIAL_NUMBER_VALUE +", 名称:"+MAIN_MATERIAL_NAME_VALUE+", 视图:"+MAIN_MATERIAL_VIEW_VALUE +"]");
			    			stringbuffer.append("父子关系中   - ");
			    			stringbuffer.append("新替代料[编码:"+NEW_SUBSTITUTE_MATERIAL_NUMBER_VALUE +", 名称:"+NEW_SUBSTITUTE_MATERIAL_NAME_VALUE+", 视图:"+NEW_SUBSTITUTE_MATERIAL_VIEW_VALUE +"]");
			    			stringbuffer.append("替换");
			    			stringbuffer.append("原替代料[编码:"+OLD_SUBSTITUTE_MATERIAL_NUMBER_VALUE +", 名称:"+OLD_SUBSTITUTE_MATERIAL_NAME_VALUE+", 视图:"+OLD_SUBSTITUTE_MATERIAL_VIEW_VALUE +"]");
			    			stringbuffer.append("[数量:"+quantityStr+", 单位:"+ QuantityUnit.toQuantityUnit(unit).getDisplay(locale)+"]");
			    			outLogInfo(stringbuffer.toString());
			    			
	    	    		}
		    			//store changeAfter
						try {
							MassChangeUtil.addChangeObjs(ca,changeAfter,"wt.change2.ChangeRecord2");
						} catch (Exception e) {
							e.printStackTrace();
						}
			    	}
	    	    }
	    	    outLogInfo("结束执行替代料批量变更...");
	    	    
		    } else if(SUBSTITUTE_MASSCHANGE_TYPE_VALUE.equalsIgnoreCase("批量新增替代料")) {	    	
		    	String MAIN_MATERIAL_NUMBER_VALUE = (String)(nmcommandbean.getText().get("MAIN_MATERIAL$label$"));
		    	String MAIN_MATERIAL_NAME_VALUE = (String)(nmcommandbean.getText().get("MAIN_MATERIAL_NAME"));
		    	String MAIN_MATERIAL_VIEW_VALUE = (String)(nmcommandbean.getText().get("MAIN_MATERIAL_VIEW"));
		    	String SUBSTITUTE_MATERIAL_NUMBER_VALUE = (String)(nmcommandbean.getText().get("SUBSTITUTE_MATERIAL$label$"));
		    	String SUBSTITUTE_MATERIAL_NAME_VALUE = (String)(nmcommandbean.getText().get("SUBSTITUTE_MATERIAL_NAME"));
		    	String SUBSTITUTE_MATERIAL_VIEW_VALUE = (String)(nmcommandbean.getText().get("SUBSTITUTE_MATERIAL_VIEW"));
		    	String QUANTITY_VALUE = (String)(nmcommandbean.getText().get("QUANTITY"));
		    	
		    	outLogInfo("开始执行替代料批量变更...");
		    	outLogInfo("输出表单数据开始...");
		    	outLogInfo("替代料批量变更类型   - 批量新增替代料");
		    	outLogInfo("主料   - "+ MAIN_MATERIAL_NUMBER_VALUE);
		    	outLogInfo("主料名称   - "+ MAIN_MATERIAL_NAME_VALUE);
		    	outLogInfo("主料视图   - "+ MAIN_MATERIAL_VIEW_VALUE);
		    	outLogInfo("替代料   - "+ SUBSTITUTE_MATERIAL_NUMBER_VALUE);
		    	outLogInfo("替代料名称   - "+ SUBSTITUTE_MATERIAL_NAME_VALUE);
		    	outLogInfo("替代料视图   - "+ SUBSTITUTE_MATERIAL_VIEW_VALUE);
		    	outLogInfo("数量   - "+ QUANTITY_VALUE);
		    	outLogInfo("输出表单数据结束...");
		    	
		    	Vector lines = MassChangeProcessorUtil.getNoYieldSelectedData(nmcommandbean, MASSCHANGE_RESULT_TABLEID2);
	    	    outDebugInfo("lines.size()="+lines.size());
	    	    if(lines.size()!=0) {
			    	if(SUBSTITUTE_MATERIAL_NUMBER_VALUE!=null && SUBSTITUTE_MATERIAL_NUMBER_VALUE!="" && SUBSTITUTE_MATERIAL_VIEW_VALUE!="" && SUBSTITUTE_MATERIAL_VIEW_VALUE!=null &&
			    		MAIN_MATERIAL_NUMBER_VALUE!=null && MAIN_MATERIAL_NUMBER_VALUE!="" && MAIN_MATERIAL_VIEW_VALUE!=null && MAIN_MATERIAL_VIEW_VALUE!="") {
			    		
			    		WTPart substituteMaterial = CoreUtil.getWTPartByNumberAndView(SUBSTITUTE_MATERIAL_NUMBER_VALUE, SUBSTITUTE_MATERIAL_VIEW_VALUE);
			    		WTPart mainMaterial = CoreUtil.getWTPartByNumberAndView(MAIN_MATERIAL_NUMBER_VALUE, MAIN_MATERIAL_VIEW_VALUE);
			    		WTPartMaster substituteMaterialMaster = (WTPartMaster) substituteMaterial.getMaster();
			    		WTPartMaster mainMaterialMaster = (WTPartMaster) mainMaterial.getMaster();
			    		
			    		Vector changeAfter = new Vector();
	    	    		for(int i=0; i<lines.size(); i++) {
	    	    			Vector line = (Vector)lines.get(i);
			    			String number = (String) line.get(0);
			    			String view = (String)line.get(1);
			    			String quantityStr = (String)line.get(2);
			    			String unit = (String)line.get(3);
			    			//String yield = (String)line.get(4);
			    			//检出对象
			    			WTPart checkoutParent = MassChangeProcessorUtil.getCheckoutParent(number, view, ca);			    			
			    			
			    			Vector usageLinks = MassChangeUtil.getPartUsageLink(checkoutParent, mainMaterial);
			    			WTPartUsageLink usageLink = (WTPartUsageLink)usageLinks.get(0);
			    			double quantitDouble = Double.parseDouble(quantityStr);
			    			SubstituteQuantity substituteQuantity0 = SubstituteQuantity.newSubstituteQuantity(quantitDouble, QuantityUnit.toQuantityUnit(unit));
			    			WTPartSubstituteLink substitutelink0 = WTPartSubstituteLink.newWTPartSubstituteLink(usageLink, substituteMaterialMaster);
			    			substitutelink0.setQuantity(substituteQuantity0);
			    			PersistenceServerHelper.manager.insert(substitutelink0);
			    			CoreUtil.checkinObject(checkoutParent, "MASSCHANGE_ADD_SUBSTITUTE");
			    			changeAfter.add(checkoutParent);
			    			
			    			//输出数据处理log
		    				StringBuffer stringbuffer = new StringBuffer();
			    			stringbuffer.append("父件[编码:"+number+", 名称:"+checkoutParent.getName()+", 视图:"+view +"]");
			    			stringbuffer.append(" - ");
			    			stringbuffer.append("主料[编码:"+MAIN_MATERIAL_NUMBER_VALUE +", 名称:"+MAIN_MATERIAL_NAME_VALUE+", 视图:"+MAIN_MATERIAL_VIEW_VALUE +"]");
			    			stringbuffer.append("父子关系中   - ");
			    			stringbuffer.append("新增替代料[编码:"+SUBSTITUTE_MATERIAL_NUMBER_VALUE +", 名称:"+SUBSTITUTE_MATERIAL_NAME_VALUE+", 视图:"+SUBSTITUTE_MATERIAL_VIEW_VALUE +"]");
			    			stringbuffer.append("[数量:"+quantityStr+", 单位:"+ QuantityUnit.toQuantityUnit(unit).getDisplay(locale)+"]");
			    			outLogInfo(stringbuffer.toString());
			    			
	    	    		}
		    			//store changeAfter
						try {
							MassChangeUtil.addChangeObjs(ca,changeAfter,"wt.change2.ChangeRecord2");
						} catch (Exception e) {
							e.printStackTrace();
						}
			    	}
	    	    }
	    	    outLogInfo("结束执行替代料批量变更...");
		    } else if(SUBSTITUTE_MASSCHANGE_TYPE_VALUE.equalsIgnoreCase("批量移除替代料")) {
		    	String MAIN_MATERIAL_NUMBER_VALUE = (String)(nmcommandbean.getText().get("MAIN_MATERIAL$label$"));
		    	String MAIN_MATERIAL_NAME_VALUE = (String)(nmcommandbean.getText().get("MAIN_MATERIAL_NAME"));
		    	String MAIN_MATERIAL_VIEW_VALUE = (String)(nmcommandbean.getText().get("MAIN_MATERIAL_VIEW"));
		    	String SUBSTITUTE_MATERIAL_NUMBER_VALUE = (String)(nmcommandbean.getText().get("SUBSTITUTE_MATERIAL$label$"));
		    	String SUBSTITUTE_MATERIAL_NAME_VALUE = (String)(nmcommandbean.getText().get("SUBSTITUTE_MATERIAL_NAME"));
		    	String SUBSTITUTE_MATERIAL_VIEW_VALUE = (String)(nmcommandbean.getText().get("SUBSTITUTE_MATERIAL_VIEW"));
		    	String QUANTITY_VALUE = (String)(nmcommandbean.getText().get("QUANTITY"));
		    	
		    	outLogInfo("开始执行替代料批量变更...");
		    	outLogInfo("输出表单数据开始...");
		    	outLogInfo("替代料批量变更类型   - 批量移除替代料");
		    	outLogInfo("主料   - "+ MAIN_MATERIAL_NUMBER_VALUE);
		    	outLogInfo("主料名称   - "+ MAIN_MATERIAL_NAME_VALUE);
		    	outLogInfo("主料视图   - "+ MAIN_MATERIAL_VIEW_VALUE);
		    	outLogInfo("替代料   - "+ SUBSTITUTE_MATERIAL_NUMBER_VALUE);
		    	outLogInfo("替代料名称   - "+ SUBSTITUTE_MATERIAL_NAME_VALUE);
		    	outLogInfo("替代料视图   - "+ SUBSTITUTE_MATERIAL_VIEW_VALUE);
		    	outLogInfo("数量   - "+ QUANTITY_VALUE);
		    	outLogInfo("输出表单数据结束...");
		    	
		    	Vector lines = MassChangeProcessorUtil.getNoYieldSelectedData(nmcommandbean, MASSCHANGE_RESULT_TABLEID2);
	    	    outDebugInfo("lines.size()="+lines.size());
	    	    if(lines.size()!=0) {
			    	if(SUBSTITUTE_MATERIAL_NUMBER_VALUE!=null && SUBSTITUTE_MATERIAL_NUMBER_VALUE!="" && SUBSTITUTE_MATERIAL_VIEW_VALUE!="" && SUBSTITUTE_MATERIAL_VIEW_VALUE!=null &&
			    		MAIN_MATERIAL_NUMBER_VALUE!=null && MAIN_MATERIAL_NUMBER_VALUE!="" && MAIN_MATERIAL_VIEW_VALUE!=null && MAIN_MATERIAL_VIEW_VALUE!="") {
			    		
			    		WTPart substituteMaterial = CoreUtil.getWTPartByNumberAndView(SUBSTITUTE_MATERIAL_NUMBER_VALUE, SUBSTITUTE_MATERIAL_VIEW_VALUE);
			    		WTPart mainMaterial = CoreUtil.getWTPartByNumberAndView(MAIN_MATERIAL_NUMBER_VALUE, MAIN_MATERIAL_VIEW_VALUE);
			    		WTPartMaster substituteMaterialMaster = (WTPartMaster) substituteMaterial.getMaster();
			    		WTPartMaster mainMaterialMaster = (WTPartMaster) mainMaterial.getMaster();
			    		
			    		Vector changeAfter = new Vector();
	    	    		for(int i=0; i<lines.size(); i++) {
	    	    			Vector line = (Vector)lines.get(i);
			    			String number = (String) line.get(0);
			    			String view = (String)line.get(1);
			    			String quantityStr = (String)line.get(2);
			    			String unit = (String)line.get(3);
			    			//String yield = (String)line.get(4);
			    			//检出对象
			    			
			    			WTPart parent = CoreUtil.getWTPartByNumberAndView(number, view);
			    			WTPart checkoutParent = null;		    			
			    			
			    			Vector usageLinks = MassChangeUtil.getPartUsageLink(parent, mainMaterial);
			    			WTPartUsageLink usageLink = (WTPartUsageLink)usageLinks.get(0);
			    			QueryResult qr = MassChangeUtil.getPartSubstituteLinks(usageLink);
			    			//记录是否有删除
			    			boolean flag = false;
			    			while(qr.hasMoreElements()) {
			    				WTPartSubstituteLink substitutelink = (WTPartSubstituteLink) qr.nextElement();
			    				WTPartMaster SubstitutesMaster = substitutelink.getSubstitutes();
			    				if(SubstitutesMaster.equals(substituteMaterialMaster)) {
			    					//PersistenceServerHelper.manager.remove(substitutelink);
			    					flag = true;
			    					break;
			    				}
			    			}
			    			if(flag) {
			    				
			    				checkoutParent = MassChangeProcessorUtil.getCheckoutParent(number, view,ca);
			    				//checkinParent = (WTPart) CoreUtil.checkinObject(checkoutParent, "MASSCHANGE_REMOVE_SUBSTITUTE");
			    				Vector usageLinks1 = MassChangeUtil.getPartUsageLink(checkoutParent, mainMaterial);
				    			WTPartUsageLink usageLink1 = (WTPartUsageLink)usageLinks1.get(0);
				    			QueryResult qr1 = MassChangeUtil.getPartSubstituteLinks(usageLink1);
				    			//记录是否有删除
				    			while(qr1.hasMoreElements()) {
				    				WTPartSubstituteLink substitutelink = (WTPartSubstituteLink) qr1.nextElement();
				    				WTPartMaster SubstitutesMaster = substitutelink.getSubstitutes();
				    				if(SubstitutesMaster.equals(substituteMaterialMaster)) {
				    					//PersistenceServerHelper.manager.remove(substitutelink);
				    					PersistenceServerHelper.manager.remove(substitutelink);
				    					break;
				    				}
				    			}
			    			} else {
			    				
			    				//输出数据处理log
			    				StringBuffer stringbuffer = new StringBuffer();
				    			stringbuffer.append("父件[编码:"+number+", 名称:"+parent.getName()+", 视图:"+view +"]");
				    			stringbuffer.append(" - ");
				    			stringbuffer.append("主料[编码:"+MAIN_MATERIAL_NUMBER_VALUE +", 名称:"+MAIN_MATERIAL_NAME_VALUE+", 视图:"+MAIN_MATERIAL_VIEW_VALUE +"]");
				    			stringbuffer.append("父子关系中   - ");
				    			stringbuffer.append("原替代料[编码:"+SUBSTITUTE_MATERIAL_NUMBER_VALUE +", 名称:"+SUBSTITUTE_MATERIAL_NAME_VALUE+", 视图:"+SUBSTITUTE_MATERIAL_VIEW_VALUE +"]");
				    			stringbuffer.append("非父子关系中的特定替代料");
				    			outLogWarn(stringbuffer.toString());
			    				continue;		    				
			    			}
			    			CoreUtil.checkinObject(checkoutParent, "MASSCHANGE_REMOVE_SUBSTITUTE");
			    			
			    			changeAfter.add(checkoutParent);
			    			
			    			//输出数据处理log
		    				StringBuffer stringbuffer = new StringBuffer();
			    			stringbuffer.append("父件[编码:"+number+", 名称:"+checkoutParent.getName()+", 视图:"+view +"]");
			    			stringbuffer.append(" - ");
			    			stringbuffer.append("主料[编码:"+MAIN_MATERIAL_NUMBER_VALUE +", 名称:"+MAIN_MATERIAL_NAME_VALUE+", 视图:"+MAIN_MATERIAL_VIEW_VALUE +"]");
			    			stringbuffer.append("父子关系中   - ");
			    			stringbuffer.append("删除替代料[编码:"+SUBSTITUTE_MATERIAL_NUMBER_VALUE +", 名称:"+SUBSTITUTE_MATERIAL_NAME_VALUE+", 视图:"+SUBSTITUTE_MATERIAL_VIEW_VALUE +"]");
			    			outLogInfo(stringbuffer.toString());
			    			
	    	    		}
		    			//store changeAfter
						try {
							MassChangeUtil.addChangeObjs(ca,changeAfter,"wt.change2.ChangeRecord2");
						} catch (Exception e) {
							e.printStackTrace();
						}
			    	}
	    	    }
	    	    outLogInfo("结束执行替代料批量变更...");
		    	
		    }
		    
	    }    
	    super.doOperation(nmcommandbean, list);
	    return formresult;
	}
}


